package com.exit66.jukebox.data;

/**
 * @author andyb
 *
 * To change this generated comment edit the template variable "typecomment":
 * Window>Preferences>Java>Templates.
 * To enable and disable the creation of type comments go to
 * Window>Preferences>Java>Code Generation.
 */

import java.sql.ResultSet;
import java.sql.SQLException;

import com.exit66.jukebox.Options;

public class RequestQueue extends BVDatabaseObject {
    
    public static final int TRACK_REQUESTED = 0;
    public static final int TRACK_ALREADY_QUEUED = 1;
    public static final int TRACK_RECENTLY_PLAYED = 2;
    public static final int ALBUM_ALL_TRACKS_REQUESTED = 3;
    public static final int ALBUM_SOME_TRACKS_REQUESTED = 4;
    public static final int ALBUM_NO_TRACKS_REQUESTED = 5;
    public static final int ARTIST_ALL_TRACKS_REQUESTED = 6;
    public static final int ARTIST_SOME_TRACKS_REQUESTED = 7;
    public static final int ARTIST_NO_TRACKS_REQUESTED = 8;
    
    private int 	_trackID = -1;
    private int 	_playOrder = -1;
    
    public int getTrackID() {
        
        return _trackID;
        
    }
    
    public void setTrackID(int newValue) {
        
        _trackID = newValue;
        
    }
    
    public int getPlayOrder() {
        
        return _playOrder;
        
    }
    
    public void setPlayOrder(int newValue) {
        
        _playOrder = newValue;
        
    }
    
    public int requestTrack(int trackID) {
        
        int nextOrder = 1;
        
        openConnection();
        
        // we're not overly concerned with duplicate play orders so don't
        // worry about multiple transactions
        
        ResultSet rs = retrieveData("SELECT COUNT(track_id) FROM request_queue WHERE track_id = " + trackID);
        
        try {
            
            rs.next();
            
            if (rs.getInt(1) == 0) {
                
                rs = retrieveData("SELECT COUNT(track_id) FROM history WHERE track_id = " + trackID + " AND track_id IN (SELECT TOP " + Options.getHistoryCount() + " track_id FROM history ORDER BY entry)");
                
                rs.next();
                
                if (rs.getInt(1) == 0) {
                    
                    rs = retrieveData("SELECT MAX(play_order) FROM request_queue");
                    
                    rs.next();
                    
                    if (rs.getString(1) != null) {
                        
                        nextOrder = rs.getInt(1) + 1;
                        
                    }
                    
                    executeStatement("INSERT INTO request_queue (track_id, play_order) VALUES (" +
                            trackID + ", " + nextOrder + ")");
                }
                else 
                {
                    return TRACK_RECENTLY_PLAYED;
                }
            }
            else 
            {
                return TRACK_ALREADY_QUEUED;
            }
        } catch (SQLException e) {
            
            System.err.println(e);
            
        }
        
        closeConnection();
        return TRACK_REQUESTED;
        
    }
    
    public int requestAlbum(int albumID) {
        
        int result = ALBUM_NO_TRACKS_REQUESTED;
        boolean failedRequest = false;
        openConnection();
        
        ResultSet rs = retrieveData("SELECT track_id FROM track WHERE album_id = " + albumID + " ORDER BY track_number, track_name");
        
        try {
            
            while (rs.next()) {
                
                if (requestTrack(rs.getInt("track_id")) == TRACK_REQUESTED) {
                    if (!failedRequest) {
                        result = ALBUM_SOME_TRACKS_REQUESTED;
                    }
                }
                else {
                    failedRequest = true;
                }                
            }
            
            if (!failedRequest) {
                result = ALBUM_ALL_TRACKS_REQUESTED;
            }
            
        } catch (SQLException e) {
            
            System.err.println(e);
            
        }
        
        return result;
    }
    
    public int requestArtist(int artistID) {
        
        int result = ARTIST_NO_TRACKS_REQUESTED;
        boolean failedRequest = false;
        openConnection();
        
        ResultSet rs = retrieveData("SELECT track_id FROM track WHERE artist_id = " + artistID);
        
        try {
            
            while (rs.next()) {
                
                if (requestTrack(rs.getInt("track_id")) == TRACK_REQUESTED) {
                     if (!failedRequest) {
                        result = ARTIST_SOME_TRACKS_REQUESTED;
                    }
                }
                else {
                    failedRequest = true;
                }
                
            }
            
            if (!failedRequest) {
                result = ARTIST_ALL_TRACKS_REQUESTED;
            }
            
        } catch (SQLException e) {
            
            System.err.println(e);
            
        }
        
        return result;
        
    }
    
    public void removeRequest(int trackID) {
        
        if (openConnection() == true) {
            
            executeStatement("DELETE FROM request_queue WHERE track_id = " + trackID);
            
            closeConnection();
            
        }
        
    }
    
    public void removeRequests(String[] tracks) {
        
        for (int i=0; i<tracks.length; i++) {
            
            if (tracks[i].length() != 0)
                removeRequest(Integer.parseInt(tracks[i]));
            
        }
        
        
    }
    
    public void empty() {
        
        if (openConnection() == true) {
            
            executeStatement("DELETE FROM request_queue");
            
            closeConnection();
            
        }
        
    }
    
    public int getNextRequest() {
        
        int trackID = -1;
        
        if (openConnection() == true) {
            
            ResultSet rs = retrieveData("select top 1 track_id from (select top " + Options.getRandomCount() +
                    " * from request_queue order by play_order) order by rand()");
            
            try {
                
                if (rs.next()) {
                    
                    trackID = rs.getInt("track_id");
                    
                    executeStatement("DELETE FROM request_queue WHERE track_id = " + trackID);
                    
                }
                
            } catch (SQLException e) {
                
                System.err.println(e);
                
            }
            
            closeConnection();
            
        }
        
        return trackID;
        
    }
    
}
